﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Web;

namespace Facebook_Graph_Toolkit {
    /// <summary>
    /// Renders a Facebook Comment Box on the page
    /// </summary>
    [Designer(typeof(CommentBoxDesigner))]
    [ToolboxData("<{0}:CommentBox runat=\"server\" />")]
    public class CommentBox : Control{
        private int _Width = 650;
        private int _Height = 150;

        /// <summary>
        /// Width of Comment Box
        /// </summary>
        [Category("Layout")]
        public int Width { get { return _Width; } set { _Width = value; } }
        /// <summary>
        /// Height of Comment Box
        /// </summary>
        [Category("Layout")]
        public int Height { get { return _Height; } set { _Height = value; } }

        /// <summary>
        /// Render the Comment Box
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer) {
            Dictionary<string, string> attributes = new Dictionary<string, string>();
            attributes.Add("src", "http://www.facebook.com/plugins/feedback.php?href=" + HttpContext.Current.Request.Url);
            attributes.Add("width", Width.ToString()+"px");
            attributes.Add("height", Height.ToString()+"px");
            attributes.Add("scrolling", "no");
            attributes.Add("frameborder", "0");
            writer.Write("<iframe " + BuildAttributeString(attributes) + ">");
            writer.Write("</iframe>");
        }
        internal static string BuildAttributeString(Dictionary<string, string> parameters) {
            string attributestring = "";
            bool firstvalue = true;
            foreach (KeyValuePair<string, string> kvp in parameters) {
                if (string.IsNullOrEmpty(kvp.Value)) continue;
                if (firstvalue) {
                    attributestring = kvp.Key + "=\"" + kvp.Value + "\"";
                    firstvalue = false;
                } else attributestring += " " + kvp.Key + "=\"" + kvp.Value + "\"";
            }
            return attributestring;
        }
    }
}
